package com.neusoft.bizcore.auth.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.dto.AuthRoleCreateUpdateDTO;
import com.neusoft.bizcore.auth.dto.AuthRoleDTO;
import com.neusoft.bizcore.auth.service.AuthRoleService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api("角色管理")
@RestController
@RequestMapping(value = "/api/w/auth/roles")
public class AuthRoleController {

    @Autowired
    private AuthRoleService authRoleService;

    @ApiOperation("查询所有角色")
    @GetMapping
    public ResultListDTO<AuthRoleDTO> index() {
        final List<AuthRoleDTO> DTOs = this.authRoleService.findAll();
        final ResultListDTO<AuthRoleDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有角色")
    @GetMapping("/p")
    public PageResultDTO<AuthRoleDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.authRoleService.search(pageable, searchable));
    }

    @ApiOperation("创建角色")
    @PostMapping
    public ResultDTO<AuthRoleDTO> create(@RequestBody final AuthRoleCreateUpdateDTO dto) {
        final AuthRoleDTO result = this.authRoleService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改角色", notes = "角色下的人和组织机构有单独API维护")
    @PutMapping("/{id}")
    public ResultDTO<AuthRoleDTO> update(@PathVariable final Long id,
            @RequestBody final AuthRoleCreateUpdateDTO dto) {
        final AuthRoleDTO result = this.authRoleService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("角色详情")
    @GetMapping("/{id}")
    public ResultDTO<AuthRoleDTO> details(@PathVariable final Long id) {
        final AuthRoleDTO dto = this.authRoleService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("根据角色编码取角色详情")
    @GetMapping("/code/{code}")
    public ResultDTO<AuthRoleDTO> detailsByCode(@PathVariable final String code) {
        final AuthRoleDTO dto = this.authRoleService.detailsByCode(code);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.authRoleService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation(value = "角色绑定用户", notes = "一次性操作全量用户。如果要单独添加、移除，调用独立API")
    @PutMapping("/{id}/bindusers")
    public ResultDTO<AuthRoleDTO> bindUsersById(@PathVariable final Long id,
            @RequestBody final List<Long> users) {
        final AuthRoleDTO result = this.authRoleService.bindUsers(id, users);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "角色添加用户", notes = "可一次性添加多个用户")
    @PutMapping("/{id}/addusers")
    public ResultDTO<AuthRoleDTO> addUsers(@PathVariable final Long id, @RequestBody final List<Long> users) {
        final AuthRoleDTO result = this.authRoleService.addUsers(id, users);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "角色移除用户", notes = "可一次性移除多个用户")
    @PutMapping("/{id}/removeusers")
    public ResultDTO<AuthRoleDTO> removeUsers(@PathVariable final Long id, @RequestBody final List<Long> users) {
        final AuthRoleDTO result = this.authRoleService.removeUsers(id, users);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "角色绑定权限", notes = "一次性操作全量权限。如果要单独添加、移除，调用独立API")
    @PutMapping("/{id}/bindpermissions")
    public ResultDTO<AuthRoleDTO> bindPermissionById(@PathVariable final Long id,
            @RequestBody final List<Long> permissions) {
        final AuthRoleDTO result = this.authRoleService.bindPermissions(id, permissions);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "角色添加权限", notes = "可一次性添加多个权限")
    @PutMapping("/{id}/addpermissions")
    public ResultDTO<AuthRoleDTO> addPermissions(@PathVariable final Long id,
            @RequestBody final List<Long> permissions) {
        final AuthRoleDTO result = this.authRoleService.addPermissions(id, permissions);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "角色移除权限", notes = "可一次性移除多个权限")
    @PutMapping("/{id}/removepermissions")
    public ResultDTO<AuthRoleDTO> removePermissions(@PathVariable final Long id,
            @RequestBody final List<Long> permissions) {
        final AuthRoleDTO result = this.authRoleService.removePermissions(id, permissions);
        return ResultDTO.success(result);
    }
}
